<?php
namespace Core\CoreComponents\LogisticManager;
use Core;

require_once(COMPONENTS_DIR . 'LogisticManager/IMaterials.php');
require_once(CONTRACTS_DIR . 'material.entity.php');

/**
* This interface provide all accessible methods on materials management module
*
* @method ServiceResponse getMaterials()
*/
class Materials implements IMaterials
{
	/**
	* Get all materials
	*
	* @return This response contains all materials
	*/
    public function getMaterials()
	{
		Core\CoreCommons\Logger::Info("Service - getMaterials");
		
		$response = null;
		
		try
		{	
			$session = Core\Datastorage\Session::Instance();
			$session->Open();
		
			$session->CreateCriteria('Material');
			$result = $session->ListResult();
			
			$response = new Core\CoreCommons\ServiceResponse($result);

			$session->Close();
			
			Core\CoreCommons\Logger::Info("Service - getMaterials completed");
		}
		catch (\Exception $ex) 
		{
			$response = Core\CoreCommons\ServiceResponse::CreateError($ex);
		}
		
		return $response;
	}
	
	/**
	* Get all material states
	*
	* @return ServiceResponse This response contains all states material
	*/
	public function getStates()
	{
		Core\CoreCommons\Logger::Info("getStates");
		
		$response = null;
		
		try
		{	
			$eventStates = new Core\CoreContracts\StateMaterial();
			
			$response = new Core\CoreCommons\ServiceResponse($eventStates->getArray());

			Core\CoreCommons\Logger::Info("Get all states is finished");
		}
		catch (\Exception $ex) 
		{
			$response = Core\CoreCommons\ServiceResponse::CreateError($ex);
		}
		
		return $response;
	}
	
	/**
	* Add a material
	*
	* @param int $state The material state
	* @param string $name The material name
	* @param string $description The material description
	* @param User $affectedUser The affected user which have the material
	* @param string $location The material location
	*
	* @return This response contains the added material
	*/
    public function addMaterial($state, $name, $description, $affectedUser, $location)
	{
		Core\CoreCommons\Logger::Info("addMaterial $state, $name, $description, $affectedUser, $location");
		
		$response = null;
		
		try
		{	
			$session = Core\Datastorage\Session::Instance();
			$session->Open();
			
			$material = Core\CoreContracts\Material::Create($state, $name, $description, $affectedUser, $location);

			$result = $session->Save($material);
			$response = new Core\CoreCommons\ServiceResponse($result);

			$session->Close();
			Core\CoreCommons\Logger::Info("Material $name added");
		}
		catch (\Exception $ex) 
		{
			$response = Core\CoreCommons\ServiceResponse::CreateError($ex);
		}
		
		return $response;
	}
	
	/**
	* Update a material
	*
	* @param int $id The material unique identifier to update
	* @param int $state The material state
	* @param string $name The material name
	* @param string $description The material description
	* @param User $affectedUser The affected user which have the material
	* @param string $location The material location
	*
	* @return This response contains the added material
	*/
    public function updateMaterial($id, $state, $name, $description, $affectedUser, $location)
	{
		Core\CoreCommons\Logger::Info("updateMaterial $id, $state, $name, $description, $affectedUser, $location");
		
		$response = null;
		
		try
		{	
			$session = Core\Datastorage\Session::Instance();
			$session->Open();
			
			$material = $session->Load('Material', $id);
			$material->State = $state;
			$material->Name = $name;
			$material->Description = $description;
			$material->AffectedUser = $affectedUser;
			$material->Location = $location;
			$material->ModificationDate = date("Y-m-d H:i:s");
			
			$fieldsToUpdate = array('State' => $material->State, 'Name' => $material->Name, 'Description' => $material->Description, 'AffectedUser' => $material->AffectedUser, 'Location' => $material->Location, 'ModificationDate' => $material->ModificationDate);
			
			$result = $session->Update($material, $fieldsToUpdate);
			$response = new Core\CoreCommons\ServiceResponse($result);

			$session->Close();
			Core\CoreCommons\Logger::Info("Material $id updated");
		}
		catch (\Exception $ex) 
		{
			$response = Core\CoreCommons\ServiceResponse::CreateError($ex);
		}
		
		return $response;
	}
	
	/**
	* Delete a material
	* 
	* @param integer $id The unique material identifier
	*
	* @return ServiceResponse This response contains the deleted material
	*/
	public function deleteMaterial($id)
	{
		Core\CoreCommons\Logger::Info("deleteMaterial $id");
		
		$response = null;
		
		try
		{	
			$session = Core\Datastorage\Session::Instance();
			$session->Open();
			
			$material = $session->Load('Material', $id);

			$result = $session->Delete($material);
			$response = new Core\CoreCommons\ServiceResponse($result);

			$session->Close();
			Core\CoreCommons\Logger::Info("Material $id deleted");
		}
		catch (\Exception $ex) 
		{
			$response = Core\CoreCommons\ServiceResponse::CreateError($ex);
		}
		
		return $response;
	}
}

?> 